﻿/*
____
 L|ypps Web controls for ASP.NET

Copyright (c) 2009 Alessandro Zifiglio, http://www.Typps.com
EMail: alessandro@Typps.com

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
namespace Typps.Tests
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;

    using Microsoft.VisualStudio.TestTools.UnitTesting;

    using Typps;

    [TestClass]
    public class UnitTestNetSpell
    {
        #region Methods

        [TestMethod]
        public void NetSpell_GetSuggestions_ListOfSuggestions()
        {
            string resourcePath = "Typps.Tests.Resources.en-US.dic";

            Stream s = Helper.GetResourceByName(this.GetType(), resourcePath);
            //punctuations included eg ! and ?
            string clientID = "editor1";
            string className = "editor1";
            NetSpellDictionary nsd = new NetSpellDictionary(string.Empty, clientID, className);
            string misspelledWord = "prblem";
            List<string> result = nsd.GetSuggestions(s, misspelledWord);
            string output = "problem";
            Assert.AreEqual(string.Join(",",result.ToArray()), output);
        }

        [TestMethod]
        public void NetSpell_SpellCheckWithHtmlEntityAttachedToWord_MisspelledJson()
        {
            string resourcePath = "Typps.Tests.Resources.en-US.dic";

            Stream s = Helper.GetResourceByName(this.GetType(), resourcePath);
            //html entity is attached to word
            string text = "<div>Hello, wht is yur <span>prblem</span>&nbsp;seriosly man</div>";
            string clientID = "editor1";
            string className = "editor1";
            NetSpellDictionary nsd = new NetSpellDictionary(string.Empty, clientID, className);
            nsd.SpellCheckComplete += new SpellCheckCompleteEventHandler(nsd_SpellCheckComplete2);
            nsd.SpellCheck(s, text);
        }

        [TestMethod]
        public void NetSpell_SpellCheckWithHtmlEntity_MisspelledJson()
        {
            string resourcePath = "Typps.Tests.Resources.en-US.dic";

            Stream s = Helper.GetResourceByName(this.GetType(), resourcePath);
            //html entity is not attached to word
            string text = "<div>Hello, wht is yur <span>prblem</span>&nbsp; seriosly man</div>";
            string clientID = "editor1";
            string className = "editor1";
            NetSpellDictionary nsd = new NetSpellDictionary(string.Empty, clientID, className);
            nsd.SpellCheckComplete += new SpellCheckCompleteEventHandler(nsd_SpellCheckComplete3);
            nsd.SpellCheck(s, text);
        }

        [TestMethod]
        public void NetSpell_SpellCheckWithPunctuations_MisspelledJson()
        {
            string resourcePath = "Typps.Tests.Resources.en-US.dic";

            Stream s = Helper.GetResourceByName(this.GetType(), resourcePath);
            //punctuations included eg ! and ?
            string text = "<div>Hello!, wht is yur <span>prblem?</span></div>";
            string clientID = "editor1";
            string className = "editor1";
            NetSpellDictionary nsd = new NetSpellDictionary(string.Empty, clientID, className);
            nsd.SpellCheckComplete += new SpellCheckCompleteEventHandler(nsd_SpellCheckComplete4);
            nsd.SpellCheck(s, text);
        }

        [TestMethod]
        public void NetSpell_SpellCheck_GetRawMisspellResults()
        {
            string resourcePath = "Typps.Tests.Resources.en-US.dic";

            Stream s = Helper.GetResourceByName(this.GetType(), resourcePath);
            //punctuations included eg ! and ?
            string text = "<div>Hello!, wht is yur <span>prblem?</span></div>";
            string clientID = "editor1";
            string className = "editor1";
            NetSpellDictionary nsd = new NetSpellDictionary(string.Empty, clientID, className);
            nsd.HtmlEnableResults = false;
            nsd.SpellCheckComplete += new SpellCheckCompleteEventHandler(nsd_SpellCheckComplete5);
            nsd.SpellCheck(s, text);
        }

        [TestMethod]
        public void NetSpell_SpellCheck_MisspelledJson()
        {
            string resourcePath = "Typps.Tests.Resources.en-US.dic";

            Stream s = Helper.GetResourceByName(this.GetType(),resourcePath);
            string text = "<div>Hello, wht is yur <span>prblem</span></div>";
            string clientID="editor1";
            string className="editor1";
            NetSpellDictionary nsd = new NetSpellDictionary(string.Empty,clientID,className);
            nsd.SpellCheckComplete += new SpellCheckCompleteEventHandler(nsd_SpellCheckComplete1);
            nsd.SpellCheck(s,text);
        }

        void nsd_SpellCheckComplete1(object sender, SpellCheckCompleteEventArgs e)
        {
            string result = e.Result;
            string output = "<div>Hello, <b id=\"editor1_1_misspellword\" class=\"editor1-misspelledword\">wht</b> is <b id=\"editor1_2_misspellword\" class=\"editor1-misspelledword\">yur</b> <span><b id=\"editor1_3_misspellword\" class=\"editor1-misspelledword\">prblem</b></span></div>$spell$checker$complete$sep$null$spell$checker$complete$sep$3";
            Assert.AreEqual(result, output);
        }

        void nsd_SpellCheckComplete2(object sender, SpellCheckCompleteEventArgs e)
        {
            string result = e.Result;
            string output = "<div>Hello, <b id=\"editor1_1_misspellword\" class=\"editor1-misspelledword\">wht</b> is <b id=\"editor1_2_misspellword\" class=\"editor1-misspelledword\">yur</b> <span>prblem</span>&nbsp;seriosly man</div>$spell$checker$complete$sep$null$spell$checker$complete$sep$3";
            Assert.AreEqual(result, output);
        }

        void nsd_SpellCheckComplete3(object sender, SpellCheckCompleteEventArgs e)
        {
            string result = e.Result;
            string output = "<div>Hello, <b id=\"editor1_1_misspellword\" class=\"editor1-misspelledword\">wht</b> is <b id=\"editor1_2_misspellword\" class=\"editor1-misspelledword\">yur</b> <span>prblem</span>&nbsp; <b id=\"editor1_3_misspellword\" class=\"editor1-misspelledword\">seriosly</b> man</div>$spell$checker$complete$sep$null$spell$checker$complete$sep$4";
            Assert.AreEqual(result, output);
        }

        void nsd_SpellCheckComplete4(object sender, SpellCheckCompleteEventArgs e)
        {
            string result = e.Result;
            string output = "<div>Hello!, <b id=\"editor1_1_misspellword\" class=\"editor1-misspelledword\">wht</b> is <b id=\"editor1_2_misspellword\" class=\"editor1-misspelledword\">yur</b> <span><b id=\"editor1_3_misspellword\" class=\"editor1-misspelledword\">prblem</b>?</span></div>$spell$checker$complete$sep$null$spell$checker$complete$sep$3";
            Assert.AreEqual(result, output);
        }

        void nsd_SpellCheckComplete5(object sender, SpellCheckCompleteEventArgs e)
        {
            string result = e.Result;
            string output = "wht,yur,prblem";
            Assert.AreEqual(result, output);
        }

        #endregion Methods
    }
}